<template>
    <view class="content">
        <!--卡项列表 -->
        <goodsTow
            ref="goodsListBox"
            @setGoodsThree="setGoodsThree"
            @addNewGoods="addNewGoods"
            @upDownClick="upDownClick"
            :tabIndex="curPage"
            :md="mdGd"
        ></goodsTow>
        <!-- 卡项详情 -->
        <goodsThree
            ref="goodsInfo"
            v-show="curIndex == 1"
            @updateListData="updateListData"
            :isNotData="isNotData"
        ></goodsThree>
        <!-- 添加服务 -->
        <goodsAdd
            ref="goodsAdd"
            v-show="curIndex == 2"
            :actType="actType"
            :is_draft="is_draft"
        ></goodsAdd>
        <!-- 上传服务列表文件 -->
        <uploadGoodsFile ref="uploadGoodsFile"></uploadGoodsFile>

        <!-- 上传下载菜单 -->
        <upAndDownLoadMenu
            ref="upAndDownLoadMenu"
            :md="mdGd"
            @showUploadGdsWin="showUploadGdsWin"
        ></upAndDownLoadMenu>
    </view>
</template>

<script>

import goodsTow from "../../components/cardManagement/GoodsTow.vue";
import goodsThree from "../../components/cardManagement/GoodsThree.vue";

import upAndDownLoadMenu from "../../components/comm/UpAndDownLoadMenu.vue";

import uploadGoodsFile from "../../components/comm/UploadGoodsFile.vue";

import lang from "../../js/conf/Lang.js";
import staticConst from "../../js/const/StaticConst.js";

import goodsAdd from "../../components/cardManagement/GoodsAdd.vue";

import eventCtrl from "../../js/event/EventCtrl.js";

import goodsCtrl from "../../js/project/GoodsCtrl.js";


export default {
    components: {
        goodsTow,
        goodsThree,
        goodsAdd,
        upAndDownLoadMenu,
        uploadGoodsFile,
    },
    data() {
        return {
            curPage: staticConst.YY_XM,
            curIndex: 1, //1,
            actType: 0, // 0:新增服务 1：编辑服务
            is_draft: "0", //0：没有草稿，1：有草稿
            isNotData: false, // 没有数据
            isClickUpDownBtn: false,
            mdGd: staticConst.YY_XM, //模块
        };
    },
    onLoad() {
	},

    onShow() {
        this.addEvent();
    },

    onHide() {
        this.removeEvent();
    },

    onUnload() {
        this.removeEvent();
    },

    onReady() {
		this.$refs.goodsListBox.initData();
    },

    watch: {
        curIndex(value) {
            if (value == 1) {
                this.is_draft = "0";
            }
        },
    },
    methods: {
        /****显示上传服务列表窗口****/
        showUploadGdsWin() {
            this.$refs.uploadGoodsFile.show("show");
        },

        /*****显示上传下载菜单****/
        upDownClick() {
            this.isClickUpDownBtn = true;
            this.$refs.upAndDownLoadMenu.show();
        },

        removeEvent() {
            uni.$off(eventCtrl.EDIT_GOODS_EVENT);
            uni.$off(eventCtrl.ADD_GOODS_EVENT_PROJECT_SUCCESS);
            uni.$off("get_img_data");
        },

        addEvent() {
            uni.$on(eventCtrl.EDIT_GOODS_EVENT, this.addNewGoods);
            uni.$on(eventCtrl.OPERATION_CAILBACK, this.operationCallback);
            uni.$on(
                eventCtrl.ADD_GOODS_EVENT_PROJECT_SUCCESS,
                this.addGoodSuccess
            );
            uni.$on("get_img_data", this.getimgData);
        },
        getimgData(data) {
            this.$refs.goodsAdd.getimgData(data);
        },
        // 批量操作后的回调
        operationCallback() {
            this.curIndex = 1;
        },
        /***点击服务列表处理**/
        setGoodsThree(res) {
            let that = this;
            if (!res) {
                this.isNotData = true;
                return;
            }
            this.isNotData = false;
            if (this.curIndex == 2) {
                let data = {};
                data.confirm = lang.saveTxt;
                data.cancel = lang.noSave;
                data.showCancel = true;
                if (this.actType != staticConst.ACT_GOODS_EIDT)
                    data.content = lang.exitEditGoodsTipDraft;
                else data.content = lang.exitEditGoodsTip;
                data.data = res.id;
                lang.showWin(data, async (result) => {
                    if (result.confirm) {
                        if (this.actType != staticConst.ACT_GOODS_EIDT)
                            goodsCtrl.saveDerift();
                        else {
                            this.$refs.goodsAdd.onSaveGds(false); //编辑服务保存服务
                        }
                    } else {
                        this.$refs.goodsAdd.clearPageData();
                    }
                    this.curIndex = 1;
                    this.$refs.goodsInfo.loadGoodsInfo(res.id);
                });
            } else {
                this.$refs.goodsInfo.loadGoodsInfo(res.id);
                this.curIndex = 1;
            }
        },

        /***添加新的服务***/
        async addNewGoods(data) {
            let that = this;
            if (this.curIndex == 2) {
                let obj = {};
                obj.confirm = lang.saveTxt;
                obj.cancel = lang.noSave;
                obj.showCancel = true;
                if (this.actType == staticConst.ACT_GOODS_ADD) {
                    obj.content = lang.exitEditGoodsTipDraft;
                } else {
                    obj.content = lang.exitEditGoodsTip;
                }
                lang.showWin(obj, callBack);
            } else {
				addGoods()
                // if (data.actType == staticConst.ACT_GOODS_EIDT) {
                //     addGoods();
                // } else {
                //     let draft = await goodsCtrl.getDerift();
                //     if (draft) {
                //         checkDerift();
                //     } else {
                //         addGoods();
                //     }
                // }
            }

            function draftCallBack(res) {
                if (res && res.confirm) {
                    goodsCtrl.editDraftToGoods(res.data);
                    that.actType = data.actType;
                    that.is_draft = "1";
                    that.curIndex = 2;
                    return;
                } else {
                    that.$refs.goodsAdd.clearPageData();
                    that.is_draft = "0";
                    goodsCtrl.clearDerift(); //清除草稿
                }
                addGoods();
            }

            async function callBack(res) {
                if (res && res.confirm) {
                    //保存草稿
                    if (that.actType == staticConst.ACT_GOODS_EIDT) {
                        // goodsCtrl.saveGoods(false);
                        that.$refs.goodsAdd.onSaveGds(false); //编辑服务保存服务
                        //
                        let draft = await goodsCtrl.getDerift(); //判断有无草稿
                        if (draft) {
                            //有草稿编辑草稿
                            checkDerift();
                            return;
                        }
                    } else {
                        if (data.actType == staticConst.ACT_GOODS_ADD)
                            //添加服务保存草稿
                            goodsCtrl.saveDerift();
                    }
                } else {
                    let draft = await goodsCtrl.getDerift(); //判断有无草稿
                    if (draft) {
                        //有草稿编辑草稿
                        checkDerift();
                        return;
                    }
                    goodsCtrl.clearDerift(); //清除草稿
                }
                addGoods();
            }

            /*****提示编辑草稿******/
            function checkDerift() {
                let obj = {};
                obj.content = lang.hasDraftTip;
                obj.confirm = lang.editDraftTxt;
                obj.cancel = lang.noEdDraftTxt;
                obj.data = data;
                obj.showCancel = true;
                lang.showWin(obj, draftCallBack);
            }

            /*****显示添加服务******/
            function addGoods() {
                that.actType = data.actType;
                goodsCtrl.setData(data);
                that.curIndex = 2;
                that.$refs.goodsAdd.init()
            }
        },

        /**********添加服务成功***********/
        addGoodSuccess(data) {
            console.log(" /**********添加服务成功***********/", data);
            if (data.isShowGgInfo) this.curIndex = 1;
            let isUpdate = data.act == "" ? true : false;
            if (!isUpdate) {
                if (
                    this.$refs.goodsListBox &&
                    this.$refs.goodsListBox.addGoodsSuccess
                )
                    this.$refs.goodsListBox.addGoodsSuccess();
            }
            if (this.$refs.goodsInfo && this.$refs.goodsInfo.loadGoodsInfo)
                this.$refs.goodsInfo.loadGoodsInfo(data.id, isUpdate);
        },

        /******编辑服务返回更新列表数据******/
        updateListData(gdInfo) {
            this.$refs.goodsListBox.updateListData(gdInfo);
        },
    },
};
</script>
<style lang="scss">
.content {
    display: flex;
    justify-content: flex-start;
    min-width: 820px;
    height: 100%;
    position: relative;
}
</style>